package com.lam.system.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.system.mapper.SysDeptUserMapper;
import com.lam.system.entity.SysDeptUser;
import com.lam.system.param.SysDeptUserParam;
import com.lam.system.service.SysDeptUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 组织机构用户Service业务层处理
 * 
 * @author admin
 * @date 2022-10-18
 */
@Service
public class SysDeptUserServiceImpl implements SysDeptUserService {


    @Autowired
    private SysDeptUserMapper sysDeptUserMapper;

    /**
     * 查询组织机构用户
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysDeptUser selectById(String id) {

        return sysDeptUserMapper.selectById(id);
    }

    /**
     * 查询组织机构用户列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysDeptUser> selectList(SysDeptUserParam param) {
    
        QueryWrapper<SysDeptUser> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
            });
        }
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(Objects.nonNull(param.getDeptId()), "dept_id", param.getDeptId());

        return sysDeptUserMapper.selectList(queryWrapper);
    }

    /**
     * 新增组织机构用户
     * 
     * @param sysDeptUser 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysDeptUser sysDeptUser) {
    
        return sysDeptUserMapper.insert(sysDeptUser);
    }
    
    /**
     * 新增组织机构用户
     * 
     * @param sysDeptUser 
     * @return 
     */
    @Override
    @Transactional
    public int saveByDept(SysDeptUserParam param) {
    	
        SysDeptUser sysDeptUser = new SysDeptUser();
        sysDeptUser.setDeptId(param.getDeptId());
        int n = 0;
        String[] userIdArr = param.getUserId().split(",");
        for (String userId : userIdArr) {
        	sysDeptUser.setUserId(userId);
        	sysDeptUser.setId(null);
        	n += sysDeptUserMapper.insert(sysDeptUser);
		}
        
        return n;
    }

    /**
     * 删除组织机构用户信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteByDept(String deptId, String userId) {
    	
    	if(StringUtils.isBlank(deptId)) {
    		return 0;
    	}
    	
    	return sysDeptUserMapper.deleteByDeptId(deptId, StringUtils.str2List(userId));
    }
    
    /**
     * 删除组织机构用户信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteByUser(String userId, String deptId) {
    	
    	if(StringUtils.isBlank(userId)) {
    		return 0;
    	}
    	
    	return sysDeptUserMapper.deleteByUserId(userId, StringUtils.str2List(deptId));
    }
    
    
}
